home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / gr564s.zip / SRC / RCSSYN.C < prev    next >
C/C++ Source or Header  |  1992-09-05  |  15KB  |  657 lines

  1. /*
  2.  *                     RCS file input
  3.  */
  4. /*********************************************************************************
  5.  *                       Syntax Analysis.
  6.  *                       Keyword table
  7.  *                       Testprogram: define SYNTEST
  8.  *                       Compatibility with Release 2: define COMPAT2=1
  9.  *********************************************************************************
  10.  */
  11.  
  12. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  13.    Copyright 1990, 1991, 1992 by Paul Eggert
  14.    Distributed under license by the Free Software Foundation, Inc.
  15.  
  16. This file is part of RCS.
  17.  
  18. RCS is free software; you can redistribute it and/or modify
  19. it under the terms of the GNU General Public License as published by
  20. the Free Software Foundation; either version 2, or (at your option)
  21. any later version.
  22.  
  23. RCS is distributed in the hope that it will be useful,
  24. but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  26. GNU General Public License for more details.
  27.  
  28. You should have received a copy of the GNU General Public License
  29. along with RCS; see the file COPYING.  If not, write to
  30. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  31.  
  32. Report problems and direct all questions to:
  33.  
  34.     rcs-bugs@cs.purdue.edu
  35.  
  36. */
  37.  
  38.  
  39. /* $Log: rcssyn.c,v $
  40.  * Revision 5.11  1992/07/28  16:12:44  eggert
  41.  * Avoid `unsigned'.  Statement macro names now end in _.
  42.  *
  43.  * Revision 5.10  1992/01/24  18:44:19  eggert
  44.  * Move put routines to rcsgen.c.
  45.  *
  46.  * Revision 5.9  1992/01/06  02:42:34  eggert
  47.  * ULONG_MAX/10 -> ULONG_MAX_OVER_10
  48.  * while (E) ; -> while (E) continue;
  49.  *
  50.  * Revision 5.8  1991/08/19  03:13:55  eggert
  51.  * Tune.
  52.  *
  53.  * Revision 5.7  1991/04/21  11:58:29  eggert
  54.  * Disambiguate names on shortname hosts.
  55.  * Fix errno bug.  Add MS-DOS support.
  56.  *
  57.  * Revision 5.6  1991/02/28  19:18:51  eggert
  58.  * Fix null termination bug in reporting keyword expansion.
  59.  *
  60.  * Revision 5.5  1991/02/25  07:12:44  eggert
  61.  * Check diff output more carefully; avoid overflow.
  62.  *
  63.  * Revision 5.4  1990/11/01  05:28:48  eggert
  64.  * When ignoring unknown phrases, copy them to the output RCS file.
  65.  * Permit arbitrary data in logs and comment leaders.
  66.  * Don't check for nontext on initial checkin.
  67.  *
  68.  * Revision 5.3  1990/09/20  07:58:32  eggert
  69.  * Remove the test for non-text bytes; it caused more pain than it cured.
  70.  *
  71.  * Revision 5.2  1990/09/04  08:02:30  eggert
  72.  * Parse RCS files with no revisions.
  73.  * Don't strip leading white space from diff commands.  Count RCS lines better.
  74.  *
  75.  * Revision 5.1  1990/08/29  07:14:06  eggert
  76.  * Add -kkvl.  Clean old log messages too.
  77.  *
  78.  * Revision 5.0  1990/08/22  08:13:44  eggert
  79.  * Try to parse future RCS formats without barfing.
  80.  * Add -k.  Don't require final newline.
  81.  * Remove compile-time limits; use malloc instead.
  82.  * Don't output branch keyword if there's no default branch,
  83.  * because RCS version 3 doesn't understand it.
  84.  * Tune.  Remove lint.
  85.  * Add support for ISO 8859.  Ansify and Posixate.
  86.  * Check that a newly checked-in file is acceptable as input to 'diff'.
  87.  * Check diff's output.
  88.  *
  89.  * Revision 4.6  89/05/01  15:13:32  narten
  90.  * changed copyright header to reflect current distribution rules
  91.  * 
  92.  * Revision 4.5  88/08/09  19:13:21  eggert
  93.  * Allow cc -R; remove lint.
  94.  * 
  95.  * Revision 4.4  87/12/18  11:46:16  narten
  96.  * more lint cleanups (Guy Harris)
  97.  * 
  98.  * Revision 4.3  87/10/18  10:39:36  narten
  99.  * Updating version numbers. Changes relative to 1.1 actually relative to
  100.  * 4.1
  101.  * 
  102.  * Revision 1.3  87/09/24  14:00:49  narten
  103.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  104.  * warnings)
  105.  * 
  106.  * Revision 1.2  87/03/27  14:22:40  jenkins
  107.  * Port to suns
  108.  * 
  109.  * Revision 4.1  83/03/28  11:38:49  wft
  110.  * Added parsing and printing of default branch.
  111.  * 
  112.  * Revision 3.6  83/01/15  17:46:50  wft
  113.  * Changed readdelta() to initialize selector and log-pointer.
  114.  * Changed puttree to check for selector==DELETE; putdtext() uses DELNUMFORM.
  115.  *
  116.  * Revision 3.5  82/12/08  21:58:58  wft
  117.  * renamed Commentleader to Commleader.
  118.  *
  119.  * Revision 3.4  82/12/04  13:24:40  wft
  120.  * Added routine gettree(), which updates keeplock after reading the
  121.  * delta tree.
  122.  *
  123.  * Revision 3.3  82/11/28  21:30:11  wft
  124.  * Reading and printing of Suffix removed; version COMPAT2 skips the
  125.  * Suffix for files of release 2 format. Fixed problems with printing nil.
  126.  *
  127.  * Revision 3.2  82/10/18  21:18:25  wft
  128.  * renamed putdeltatext to putdtext.
  129.  *
  130.  * Revision 3.1  82/10/11  19:45:11  wft
  131.  * made sure getc() returns into an integer.
  132.  */
  133.  
  134.  
  135.  
  136. /* version COMPAT2 reads files of the format of release 2 and 3, but
  137.  * generates files of release 3 format. Need not be defined if no
  138.  * old RCS files generated with release 2 exist.
  139.  */
  140. /* version SYNTEST inputs a RCS file and then prints out its internal
  141.  * data structures.
  142. */
  143.  
  144. #include "rcsbase.h"
  145.  
  146. libId(synId, "$Id: rcssyn.c,v 5.11 1992/07/28 16:12:44 eggert Exp $")
  147.  
  148. /* forward */
  149. static char const *getkeyval P((char const*,enum tokens,int));
  150. static int strn2expmode P((char const*,size_t));
  151.  
  152. /* keyword table */
  153.  
  154. char const
  155.     Kaccess[]   = "access",
  156.     Kauthor[]   = "author",
  157.     Kbranch[]   = "branch",
  158.     Kcomment[]  = "comment",
  159.     Kdate[]     = "date",
  160.     Kdesc[]     = "desc",
  161.     Kexpand[]   = "expand",
  162.     Khead[]     = "head",
  163.     Klocks[]    = "locks",
  164.     Klog[]      = "log",
  165.     Knext[]     = "next",
  166.     Kstate[]    = "state",
  167.     Kstrict[]   = "strict",
  168.     Ksymbols[]  = "symbols",
  169.     Ktext[]     = "text";
  170.  
  171. static char const
  172. #if COMPAT2
  173.     Ksuffix[]   = "suffix",
  174. #endif
  175.     K_branches[]= "branches";
  176.  
  177. static struct buf Commleader;
  178. struct cbuf Comment;
  179. struct cbuf Ignored;
  180. struct access   * AccessList;
  181. struct assoc    * Symbols;
  182. struct lock     * Locks;
  183. int          Expand;
  184. int               StrictLocks;
  185. struct hshentry * Head;
  186. char const      * Dbranch;
  187. int TotalDeltas;
  188.  
  189.  
  190.     static void
  191. getsemi(key)
  192.     char const *key;
  193. /* Get a semicolon to finish off a phrase started by KEY.  */
  194. {
  195.     if (!getlex(SEMI))
  196.         fatserror("missing ';' after '%s'", key);
  197. }
  198.  
  199.     static struct hshentry *
  200. getdnum()
  201. /* Get a delta number.  */
  202. {
  203.     register struct hshentry *delta = getnum();
  204.     if (delta && countnumflds(delta->num)&1)
  205.         fatserror("%s isn't a delta number", delta->num);
  206.     return delta;
  207. }
  208.  
  209.  
  210.     void
  211. getadmin()
  212. /* Read an <admin> and initialize the appropriate global variables.  */
  213. {
  214.     register char const *id;
  215.         struct access   * newaccess;
  216.         struct assoc    * newassoc;
  217.         struct lock     * newlock;
  218.         struct hshentry * delta;
  219.     struct access **LastAccess;
  220.     struct assoc **LastSymbol;
  221.     struct lock **LastLock;
  222.     struct buf b;
  223.     struct cbuf cb;
  224.  
  225.         TotalDeltas=0;
  226.  
  227.     getkey(Khead);
  228.     Head = getdnum();
  229.     getsemi(Khead);
  230.  
  231.     Dbranch = nil;
  232.     if (getkeyopt(Kbranch)) {
  233.         if ((delta = getnum()))
  234.             Dbranch = delta->num;
  235.         getsemi(Kbranch);
  236.         }
  237.  
  238.  
  239. #if COMPAT2
  240.         /* read suffix. Only in release 2 format */
  241.     if (getkeyopt(Ksuffix)) {
  242.                 if (nexttok==STRING) {
  243.             readstring(); nextlex(); /* Throw away the suffix.  */
  244.         } else if (nexttok==ID) {
  245.                         nextlex();
  246.                 }
  247.         getsemi(Ksuffix);
  248.         }
  249. #endif
  250.  
  251.     getkey(Kaccess);
  252.     LastAccess = &AccessList;
  253.         while (id=getid()) {
  254.         newaccess = ftalloc(struct access);
  255.                 newaccess->login = id;
  256.         *LastAccess = newaccess;
  257.         LastAccess = &newaccess->nextaccess;
  258.         }
  259.     *LastAccess = nil;
  260.     getsemi(Kaccess);
  261.  
  262.     getkey(Ksymbols);
  263.     LastSymbol = &Symbols;
  264.         while (id = getid()) {
  265.                 if (!getlex(COLON))
  266.             fatserror("missing ':' in symbolic name definition");
  267.                 if (!(delta=getnum())) {
  268.             fatserror("missing number in symbolic name definition");
  269.                 } else { /*add new pair to association list*/
  270.             newassoc = ftalloc(struct assoc);
  271.                         newassoc->symbol=id;
  272.             newassoc->num = delta->num;
  273.             *LastSymbol = newassoc;
  274.             LastSymbol = &newassoc->nextassoc;
  275.                 }
  276.         }
  277.     *LastSymbol = nil;
  278.     getsemi(Ksymbols);
  279.  
  280.     getkey(Klocks);
  281.     LastLock = &Locks;
  282.         while (id = getid()) {
  283.                 if (!getlex(COLON))
  284.             fatserror("missing ':' in lock");
  285.         if (!(delta=getdnum())) {
  286.             fatserror("missing number in lock");
  287.                 } else { /*add new pair to lock list*/
  288.             newlock = ftalloc(struct lock);
  289.                         newlock->login=id;
  290.                         newlock->delta=delta;
  291.             *LastLock = newlock;
  292.             LastLock = &newlock->nextlock;
  293.                 }
  294.         }
  295.     *LastLock = nil;
  296.     getsemi(Klocks);
  297.  
  298.     if ((StrictLocks = getkeyopt(Kstrict)))
  299.         getsemi(Kstrict);
  300.  
  301.     Comment.size = 0;
  302.     if (getkeyopt(Kcomment)) {
  303.         if (nexttok==STRING) {
  304.             Comment = savestring(&Commleader);
  305.             nextlex();
  306.         }
  307.         getsemi(Kcomment);
  308.         }
  309.  
  310.     Expand = KEYVAL_EXPAND;
  311.     if (getkeyopt(Kexpand)) {
  312.         if (nexttok==STRING) {
  313.             bufautobegin(&b);
  314.             cb = savestring(&b);
  315.             if ((Expand = strn2expmode(cb.string,cb.size)) < 0)
  316.                 fatserror("unknown expand mode %.*s",
  317.                 (int)cb.size, cb.string
  318.                 );
  319.             bufautoend(&b);
  320.             nextlex();
  321.         }
  322.         getsemi(Kexpand);
  323.         }
  324.     Ignored = getphrases(Kdesc);
  325. }
  326.  
  327. char const *const expand_names[] = {
  328.     /* These must agree with *_EXPAND in rcsbase.h.  */
  329.     "kv","kvl","k","v","o",
  330.     0
  331. };
  332.  
  333.     int
  334. str2expmode(s)
  335.     char const *s;
  336. /* Yield expand mode corresponding to S, or -1 if bad.  */
  337. {
  338.     return strn2expmode(s, strlen(s));
  339. }
  340.  
  341.     static int
  342. strn2expmode(s, n)
  343.     char const *s;
  344.     size_t n;
  345. {
  346.     char const *const *p;
  347.  
  348.     for (p = expand_names;  *p;  ++p)
  349.         if (memcmp(*p,s,n) == 0  &&  !(*p)[n])
  350.             return p - expand_names;
  351.     return -1;
  352. }
  353.  
  354.  
  355.     void
  356. ignorephrase()
  357. /* Ignore a phrase introduced by a later version of RCS.  */
  358. {
  359.     warnignore();
  360.     hshenter=false;
  361.     for (;;) {
  362.         switch (nexttok) {
  363.         case SEMI: hshenter=true; nextlex(); return;
  364.         case ID:
  365.         case NUM: ffree1(NextString); break;
  366.         case STRING: readstring(); break;
  367.         default: break;
  368.         }
  369.         nextlex();
  370.     }
  371. }
  372.  
  373.  
  374.     static int
  375. getdelta()
  376. /* Function: reads a delta block.
  377.  * returns false if the current block does not start with a number.
  378.  */
  379. {
  380.         register struct hshentry * Delta, * num;
  381.     struct branchhead **LastBranch, *NewBranch;
  382.  
  383.     if (!(Delta = getdnum()))
  384.         return false;
  385.  
  386.         hshenter = false; /*Don't enter dates into hashtable*/
  387.         Delta->date = getkeyval(Kdate, NUM, false);
  388.         hshenter=true;    /*reset hshenter for revision numbers.*/
  389.  
  390.         Delta->author = getkeyval(Kauthor, ID, false);
  391.  
  392.         Delta->state = getkeyval(Kstate, ID, true);
  393.  
  394.     getkey(K_branches);
  395.     LastBranch = &Delta->branches;
  396.     while ((num = getdnum())) {
  397.         NewBranch = ftalloc(struct branchhead);
  398.                 NewBranch->hsh = num;
  399.         *LastBranch = NewBranch;
  400.         LastBranch = &NewBranch->nextbranch;
  401.         }
  402.     *LastBranch = nil;
  403.     getsemi(K_branches);
  404.  
  405.     getkey(Knext);
  406.     Delta->next = num = getdnum();
  407.     getsemi(Knext);
  408.     Delta->lockedby = nil;
  409.     Delta->log.string = 0;
  410.     Delta->selector = true;
  411.     Delta->ig = getphrases(Kdesc);
  412.         TotalDeltas++;
  413.         return (true);
  414. }
  415.  
  416.  
  417.     void
  418. gettree()
  419. /* Function: Reads in the delta tree with getdelta(), then
  420.  * updates the lockedby fields.
  421.  */
  422. {
  423.     struct lock const *currlock;
  424.  
  425.     while (getdelta())
  426.         continue;
  427.         currlock=Locks;
  428.         while (currlock) {
  429.                 currlock->delta->lockedby = currlock->login;
  430.                 currlock = currlock->nextlock;
  431.         }
  432. }
  433.  
  434.  
  435.     void
  436. getdesc(prdesc)
  437. int  prdesc;
  438. /* Function: read in descriptive text
  439.  * nexttok is not advanced afterwards.
  440.  * If prdesc is set, the text is printed to stdout.
  441.  */
  442. {
  443.  
  444.     getkeystring(Kdesc);
  445.         if (prdesc)
  446.                 printstring();  /*echo string*/
  447.         else    readstring();   /*skip string*/
  448. }
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.     static char const *
  456. getkeyval(keyword, token, optional)
  457.     char const *keyword;
  458.     enum tokens token;
  459.     int optional;
  460. /* reads a pair of the form
  461.  * <keyword> <token> ;
  462.  * where token is one of <id> or <num>. optional indicates whether
  463.  * <token> is optional. A pointer to
  464.  * the actual character string of <id> or <num> is returned.
  465.  */
  466. {
  467.     register char const *val = nil;
  468.  
  469.     getkey(keyword);
  470.         if (nexttok==token) {
  471.                 val = NextString;
  472.                 nextlex();
  473.         } else {
  474.         if (!optional)
  475.             fatserror("missing %s", keyword);
  476.         }
  477.     getsemi(keyword);
  478.         return(val);
  479. }
  480.  
  481.  
  482.     exiting void
  483. unexpected_EOF()
  484. {
  485.     faterror("unexpected EOF in diff output");
  486. }
  487.  
  488.     void
  489. initdiffcmd(dc)
  490.     register struct diffcmd *dc;
  491. /* Initialize *dc suitably for getdiffcmd(). */
  492. {
  493.     dc->adprev = 0;
  494.     dc->dafter = 0;
  495. }
  496.  
  497.     static exiting void
  498. badDiffOutput(buf)
  499.     char const *buf;
  500. {
  501.     faterror("bad diff output line: %s", buf);
  502. }
  503.  
  504.     static exiting void
  505. diffLineNumberTooLarge(buf)
  506.     char const *buf;
  507. {
  508.     faterror("diff line number too large: %s", buf);
  509. }
  510.  
  511.     int
  512. getdiffcmd(finfile, delimiter, foutfile, dc)
  513.     RILE *finfile;
  514.     FILE *foutfile;
  515.     int delimiter;
  516.     struct diffcmd *dc;
  517. /* Get a editing command output by 'diff -n' from fin.
  518.  * The input is delimited by SDELIM if delimiter is set, EOF otherwise.
  519.  * Copy a clean version of the command to fout (if nonnull).
  520.  * Yield 0 for 'd', 1 for 'a', and -1 for EOF.
  521.  * Store the command's line number and length into dc->line1 and dc->nlines.
  522.  * Keep dc->adprev and dc->dafter up to date.
  523.  */
  524. {
  525.     register int c;
  526.     declarecache;
  527.     register FILE *fout;
  528.     register char *p;
  529.     register RILE *fin;
  530.     long line1, nlines, t;
  531.     char buf[BUFSIZ];
  532.  
  533.     fin = finfile;
  534.     fout = foutfile;
  535.     setupcache(fin); cache(fin);
  536.     cachegeteof_(c, { if (delimiter) unexpected_EOF(); return -1; } )
  537.     if (delimiter) {
  538.         if (c==SDELIM) {
  539.             cacheget_(c)
  540.             if (c==SDELIM) {
  541.                 buf[0] = c;
  542.                 buf[1] = 0;
  543.                 badDiffOutput(buf);
  544.             }
  545.             uncache(fin);
  546.             nextc = c;
  547.             if (fout)
  548.                 aprintf(fout, "%c%c", SDELIM, c);
  549.             return -1;
  550.         }
  551.     }
  552.     p = buf;
  553.     do {
  554.         if (buf+BUFSIZ-2 <= p) {
  555.             faterror("diff output command line too long");
  556.         }
  557.         *p++ = c;
  558.         cachegeteof_(c, unexpected_EOF();)
  559.     } while (c != '\n');
  560.     uncache(fin);
  561.     if (delimiter)
  562.         ++rcsline;
  563.     *p = '\0';
  564.     for (p = buf+1;  (c = *p++) == ' ';  )
  565.         continue;
  566.     line1 = 0;
  567.     while (isdigit(c)) {
  568.         if (
  569.             LONG_MAX/10 < line1  ||
  570.             (t = line1 * 10,   (line1 = t + (c - '0'))  <  t)
  571.         )
  572.             diffLineNumberTooLarge(buf);
  573.         c = *p++;
  574.     }
  575.     while (c == ' ')
  576.         c = *p++;
  577.     nlines = 0;
  578.     while (isdigit(c)) {
  579.         if (
  580.             LONG_MAX/10 < nlines  ||
  581.             (t = nlines * 10,   (nlines = t + (c - '0'))  <  t)
  582.         )
  583.             diffLineNumberTooLarge(buf);
  584.         c = *p++;
  585.     }
  586.     if (c || !nlines) {
  587.         badDiffOutput(buf);
  588.     }
  589.     if (line1+nlines < line1)
  590.         diffLineNumberTooLarge(buf);
  591.     switch (buf[0]) {
  592.         case 'a':
  593.         if (line1 < dc->adprev) {
  594.             faterror("backward insertion in diff output: %s", buf);
  595.         }
  596.         dc->adprev = line1 + 1;
  597.         break;
  598.         case 'd':
  599.         if (line1 < dc->adprev  ||  line1 < dc->dafter) {
  600.             faterror("backward deletion in diff output: %s", buf);
  601.         }
  602.         dc->adprev = line1;
  603.         dc->dafter = line1 + nlines;
  604.         break;
  605.         default:
  606.         badDiffOutput(buf);
  607.     }
  608.     if (fout) {
  609.         aprintf(fout, "%s\n", buf);
  610.     }
  611.     dc->line1 = line1;
  612.     dc->nlines = nlines;
  613.     return buf[0] == 'a';
  614. }
  615.  
  616.  
  617.  
  618. #ifdef SYNTEST
  619.  
  620. char const cmdid[] = "syntest";
  621.  
  622.     int
  623. main(argc,argv)
  624. int argc; char * argv[];
  625. {
  626.  
  627.         if (argc<2) {
  628.         aputs("No input file\n",stderr);
  629.         exitmain(EXIT_FAILURE);
  630.         }
  631.     if (!(finptr = Iopen(argv[1], FOPEN_R, (struct stat*)0))) {
  632.         faterror("can't open input file %s", argv[1]);
  633.         }
  634.         Lexinit();
  635.         getadmin();
  636.     fdlock = STDOUT_FILENO;
  637.     putadmin();
  638.  
  639.         gettree();
  640.  
  641.         getdesc(true);
  642.  
  643.     nextlex();
  644.  
  645.     if (!eoflex()) {
  646.         fatserror("expecting EOF");
  647.         }
  648.     exitmain(EXIT_SUCCESS);
  649. }
  650.  
  651.  
  652. exiting void exiterr() { _exit(EXIT_FAILURE); }
  653.  
  654.  
  655. #endif
  656.  
  657.